<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\model\Assets;
use app\common\model\Statistics;
use think\facade\View;

class Desktop extends Base
{
    public function statistics(): string
    {
        $list = Statistics::select()->toArray();
        $expend = Statistics::where(['id' => 1])->find()->toArray();
        $income = Statistics::field('sum(`yesterday`) as `yesterday`, sum(`today`) as `today`, sum(`last_month`) as `last_month`, sum(`month`) as `month`, sum(`last_year`) as `last_year`, sum(`year`) as `year`, sum(`sum`) as `sum`')
            ->whereIn('id', [2])->find()->toArray();
        $rate['id'] = 4;
        $rate['title'] = '拨比率';
        $rate['key'] = 'rate';
        $rate_key = ['yesterday', 'today', 'last_month', 'month', 'last_year', 'year', 'sum'];
        foreach ($rate_key as $v) {
            if ($income[$v] > 0 && $expend[$v] > 0) {
                $rate[$v] = sprintf('%.2f', ($expend[$v] / $income[$v] * 100)) . '%';
            } else {
                $rate[$v] = '0.00%';
            }
        }
        $list[] = $rate;
        View::assign('list', $list);

        $assets = [];
        foreach (Assets::getFields(3) as $key => $field) {
            $sum = Assets::sum($key);
            $assets[] = [
                'title' => $field,
                'sum'   => sprintf('%.2f', $sum)
            ];
        }
        View::assign('assets', $assets);

        return View::fetch();
    }
}